    //
//  RootViewController.m
//  AirTicket
//
//  Created by imac.bmw on 10-12-15.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "QueryFlightViewController.h"
#import "LoginViewController.h"
#import "InfoMgr.h"
#import "CWUserInfo.h"
#import "CWUserRegisterInfo.h"
#import "CWOrderInfo.h"
#import "CWSocketByStream.h"
#import "extver.h"
InfoMgr *infoMgr;

NSUserDefaults * SaveDefaults;

RootViewController *mainViewController;
CWSocketByStream *clientSocket;

@implementation RootViewController
@synthesize table;
@synthesize delegate;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	//-----------------------socket初始化部分
	mainViewController = self;
		clientSocket = [[CWSocketByStream alloc] init];
		[clientSocket createStream:@"58.247.98.94" serverPort:7000];
	//------------------------------------------------------------------
	self.navigationItem.title = @"宜赋通手机订票";
	
	UITableView * aTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
	self.table = aTable;
	[aTable release];
	table.delegate = self;
	table.dataSource = self;
	table.scrollEnabled = NO;
	table.separatorColor = [UIColor clearColor];
	[self.view addSubview:table];
	
	infoMgr = [[InfoMgr alloc]init];
	[infoMgr loadDataList];
	
	SaveDefaults = [NSUserDefaults standardUserDefaults];
	[SaveDefaults setBool:NO forKey:@"login"];
}


- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{

	return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

	return 104;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

		NSInteger row = [indexPath row];
		static NSString *CellIdentifier = @"last";
		
		UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
		if (cell == nil) 
		{
			cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 300.0, 60.0) reuseIdentifier:CellIdentifier] autorelease];
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
		}
	if (row == 0) {
		UIImageView *im = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"index2.png"]];
		im.frame = CGRectMake(9, 2, 302, 100);
		[cell.contentView addSubview:im];
		[im release];
	}
	else if (row == 1) {
		UIImageView *im = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"index1.png"]];
		im.frame = CGRectMake(9, 2, 302, 100);
		[cell.contentView addSubview:im];
		[im release];
	}
	else if (row == 2) {
		UIImageView *im = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"index3.png"]];
		im.frame = CGRectMake(9, 2, 302, 100);
		[cell.contentView addSubview:im];
		[im release];
	}
	else if (row == 3) {
		UIImageView *im = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"index4.png"]];
		im.frame = CGRectMake(9, 2, 302, 100);
		[cell.contentView addSubview:im];
		[im release];
	}
		
		return cell;
	
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	[self.table deselectRowAtIndexPath:[self.table indexPathForSelectedRow] animated:YES];

	NSInteger row = [indexPath row];
	
	if (row == 0) {
		QueryFlightViewController *queryView = [[QueryFlightViewController alloc]init];
		[self.navigationController pushViewController:queryView animated:YES];
		[queryView release];
	}
	else if (row == 1) {
		InfoMgrViewController *controller = [[InfoMgrViewController alloc]init];
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];
	}
	else if (row == 2) {
		CardViewController *controller = [[CardViewController alloc]init];
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];
	}
	else if (row == 3) {
		OrderMgrViewController *controller = [[OrderMgrViewController alloc]init];
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];
	}
	
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/
-(void)processPackage:(NSData *)packageData
{
	NSString *returnMassage;
	NSString *context = [[NSString alloc] initWithData:packageData encoding:NSUTF8StringEncoding];
	NSArray *fields = [context componentsSeparatedByString:@"|"];
	
	NSLog(@"get :%@",context);
	
	if([(NSString *)[fields objectAtIndex:0] length]==6)
	{
		NSString *head = [(NSString *)[fields objectAtIndex:0] substringToIndex:2];
		
		if([head isEqualToString:@"RG"])												//注册返回包
		{
			if([(NSString *)[fields objectAtIndex:1] isEqualToString:@"S"])				//成功
			{
				returnMassage = [fields objectAtIndex:2];						//返回的成功信息
			}
			else																		//失败
			{
				returnMassage = [fields objectAtIndex:2];						//返回的失败信息
			}
		}
		if([head isEqualToString:@"LG"])												//登录返回包
		{
			CWUserInfo *userInfo = [[CWUserInfo alloc] init];
			[userInfo initWithString:context];
		}
		if([head isEqualToString:@"BK"])												//订票返回包
		{
			NSString *orderID = [CWSocketByStream getSub:context fromStr:@"<orderid>" toStr:@"<／orderid>"];	//获得返回的成功订单号
		}
		if([head isEqualToString:@"RZ"])												//支付返回包
		{
			if([(NSString *)[fields objectAtIndex:1] isEqualToString:@"S"])				//成功
			{
				returnMassage = [fields objectAtIndex:2];						//返回的成功信息
			}
			else																		//失败
			{
				returnMassage = [fields objectAtIndex:2];						//返回的失败信息
			}
		}
		if([head isEqualToString:@"PC"])												//修改登录密码返回包
		{
			if([(NSString *)[fields objectAtIndex:1] isEqualToString:@"S"])				//成功
			{
				returnMassage = [fields objectAtIndex:2];						//返回的成功信息
			}
			else																		//失败
			{
				returnMassage = [fields objectAtIndex:2];						//返回的失败信息
			}
		}
		if([head isEqualToString:@"PK"])												//修改支付密码返回包
		{
			if([(NSString *)[fields objectAtIndex:1] isEqualToString:@"S"])				//成功
			{
				returnMassage = [fields objectAtIndex:2];						//返回的成功信息
			}
			else																		//失败
			{
				returnMassage = [fields objectAtIndex:2];						//返回的失败信息
			}
		}
		if([head isEqualToString:@"AC"])												//增加银行卡返回包
		{
			if([(NSString *)[fields objectAtIndex:1] isEqualToString:@"S"])				//成功
			{
				returnMassage = [fields objectAtIndex:2];						//返回的成功信息
			}
			else																		//失败
			{
				returnMassage = [fields objectAtIndex:2];						//返回的失败信息
			}
		}
		if([head isEqualToString:@"MI"])												//修改个人信息返回包
		{
			if([(NSString *)[fields objectAtIndex:1] isEqualToString:@"S"])				//成功
			{
				returnMassage = [fields objectAtIndex:2];						//返回的成功信息
			}
			else																		//失败
			{
				returnMassage = [fields objectAtIndex:2];						//返回的失败信息
			}
		}
	}
	else {
		NSLog(@"接受包数据头长度错误！");
	}
	
	NSLog(@"result :%@",returnMassage);
	[context release];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

@implementation NSString(convertCode)

-(NSString *)convertCitycode {
	NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"citycode" ofType:@"plist"]];
	
	NSString *cityName = [[dic valueForKey:self] retain];
	[dic release];
	return [cityName autorelease];
	
}

-(NSString *)covertAircode{
	
	NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"aircode" ofType:@"plist"]];
	
	NSString *airName = [[dic valueForKey:self] retain];
	[dic release];
	return [airName autorelease];
	
}
-(NSString *)convertDate {
	
	NSString *myDateString = self;
	NSDateFormatter *inputFormat = [[NSDateFormatter alloc] init];
	[inputFormat setDateFormat:@"yyyy-MM-dd"]; 
	NSDate *theDate  = [inputFormat dateFromString:myDateString];
	NSDateFormatter *outputFormat = [[NSDateFormatter alloc] init];
	[outputFormat setDateFormat:@"yyyy-MM-dd"];
	NSString *returnDate = [outputFormat stringFromDate:theDate];
	return returnDate;
}
@end

//------------

